<?php

require_once __DIR__.DS.'blgData.php';

abstract class BlgEntity extends BlgData{

	protected function initializeAttributes() {
		parent::initializeAttributes();

		$this->attributes['guid'] = 0;
		$this->attributes['type'] = '';
		$this->attributes['subtype'] = 0;

		$this->attributes['owner_guid'] = 0;
		$this->attributes['container_guid'] = 0;

		$this->attributes['site_guid'] = 0;
		$this->attributes['access_id'] = ACCESS_PRIVATE;
		$this->attributes['time_created'] = 0;
		$this->attributes['time_updated'] = 0;
		$this->attributes['last_action'] = 0;
		$this->attributes['enabled'] = "no";
	}
	

	/**
	 * Update entity access id
	 * @param $access_id
	 * @return Boolean
	 */
	public function change_access($access_id){
		if(blg_entity_update_access_id($this->attributes['guid'], $access_id)){
			$this->attributes['access_id'] = $access_id;
			$this->save();
			return TRUE;
		}		
		return FALSE;
	}
	
	/**
	 * Update entity last action time
	 * @param $access_id
	 * @return Boolean
	 */	
	public function save_last_action($time){		
		if(blg_entity_last_action($this->attributes['guid'], $time)){
			$this->attributes['last_action'] = $time;
			$this->save();
			return TRUE;
		}		
		return FALSE;		
	}

	/**
	 * Update entity time updated
	 * @param $access_id
	 * @return Boolean
	 */		
	public function save_time_updated($time){
		if(blg_entity_update_time_updated($this->attributes['guid'], $time)){
			$this->attributes['time_updated'] = $time;
			return TRUE;
		}		
		return FALSE;
	}
	
	/**
	 * Alias for 'save_time_updated($time)' without $time param
	 */	
	public function save(){
		return $this->save_time_updated(time());
	}	

	/**
	 * Delete entity
	 * @see engine/clases/BlgData::delete()
	 */
	public function delete(){
		if(blg_entity_delete($this->attributes['guid'])){
			$this->attributes['enabled']= 'no';
			return TRUE;
		}
		
		return FALSE;
	}
	
	/* METADATA EXTENSION */
	
	/**
	 * Fetch the specified attribute
	 * 
	 * @param string $name The attribute to fetch
	 * @return mixed The attribute, if it exists. Otherwise, null.
	 * @author Pablo
 	 * @since 1.0
	 */
	protected function get($name){
		if(array_key_exists($name, $this->attributes)){
			return $this->attributes[$name];
		}else if(FALSE!==($value=$this->get_metadata($name))){
			return $value;	
		}
		
		return NULL;
	}
	
	/**
	 * Set the specified attribute
	 * 
	 * @param string $name The attribute to set
	 * @param mixed $value The value to set it to
	 * @author Pablo
 	 * @since 1.0 
	 */	
	protected function set($name, $value){
		if(array_key_exists($name, $this->attributes)){
			$this->attributes[$name]= $value;
		}
		else{
			$this->set_metadata($name, $value);
		}		
	}
	
	/**
	 * Get metadata of this entity
	 * @param $string $name
	 * @return mixed|FALSE Returns the metadata object value if it exists, FALSE otherwise;
	 */
	protected function get_metadata($name){
		if(FALSE!==($metadata_id=blg_metadata_exists($this->guid, $name))){
			$metadata= new BlgMetadata($metadata_id);
			return $metadata->get_value();
		}

		return FALSE;
	}
	
	/**
	 * Creates or updates a metadata of this entity
	 * @param String $name 	Metadata name 
	 * @param Mixed $value 	Metadata value
	 * @return Boolean
	 */
	protected function set_metadata($name, $value){
		// If the metadata does NOT exist
		if(FALSE===($metadata_id=blg_metadata_exists($this->guid, $name))){
			return (FALSE!==BlgMetadata::create($this->guid, $name, $value, NULL, NULL, $this->attributes['access_id']));				
		}
		// If metatada ALREADY exists
		else{
			return blg_metadata_update_value($metadata_id, $value);
		}
	}
}
